package com.mask.im.business.controller.group;

import com.mask.im.business.annotation.AesCrypto;
import com.mask.im.business.annotation.OptLog;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.model.dto.GroupChatAdminDTO;
import com.mask.im.business.model.dto.GroupChatDTO;
import com.mask.im.business.model.dto.GroupMembersDTO;
import com.mask.im.business.model.vo.*;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.service.IMGroupChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;

import static com.mask.im.business.common.constant.OptTypeConstant.SAVE;

/**
 * 群聊模块
 *
 * @author jian
 */
@Log4j2
@Api(tags = "群聊模块")
@RestController
@RequestMapping("groups")
public class IMGroupChatController {

    @Resource
    private IMGroupChatService groupChatService;


    @ApiOperation("获取群聊列表")
    @GetMapping("/list")
    public RestResponse<List<IMGroupChatVO>> getGroupList() {
        return groupChatService.getGroupList();
    }


    @AesCrypto
    @OptLog(optType = SAVE)
    @ApiOperation("创建群聊")
    @PostMapping("/create")
    public RestResponse<String> createGroupChat(@RequestBody GroupChatVO groupChatVO) {
        groupChatService.createGroupChat(groupChatVO);
        return RestResponse.ok();
    }


    @ApiOperation("获取群聊人员")
    @GetMapping("/member")
    public PageResult<GroupMembersDTO> getGroupMembers(@RequestParam("chatGroupId") Integer chatGroupId) {
        try {
            return groupChatService.getGroupMembers(chatGroupId);
        } catch (ExecutionException | InterruptedException e) {
            log.error("getGroupMembers=====================error:" + e.getMessage(), e);
        }
        return PageResult.<GroupMembersDTO>builder().data(null).total(0).code(-1).build();
    }


    @ApiOperation("判断自己是不是群主")
    @GetMapping("/isLeader")
    public RestResponse<Boolean> isGroupLeader(@RequestParam("groupChatId") Integer groupChatId) {
        return groupChatService.isGroupLeader(groupChatId);
    }


    @ApiOperation("解散群聊")
    @PutMapping("/disband/{groupChatId}")
    public RestResponse<String> disbandGroupChat(@PathVariable Integer groupChatId) {
        return groupChatService.disbandGroupChat(groupChatId);
    }

    @ApiOperation("获取群聊状态")
    @GetMapping("/groupChatStatus")
    public RestResponse<Boolean> getGroupChatStatus(@RequestParam("groupChatId") Integer groupChatId) {
        return groupChatService.getGroupChatStatus(groupChatId);
    }

    @ApiOperation("拉人入群聊")
    @PostMapping("/pullGroupChat")
    @AesCrypto
    public RestResponse<String> pullGroupChat(@RequestBody PullChatVO pullChatVO) {
        return groupChatService.pullGroupChat(pullChatVO);
    }

    @ApiOperation("获取群聊信息")
    @GetMapping("/groupChatMessage")
    public RestResponse<GroupChatDTO> groupChatMessage(@RequestParam("chatGroupId") Integer chatGroupId) {
        return groupChatService.groupChatMessage(chatGroupId);
    }

    @ApiOperation("修改群聊信息")
    @PutMapping("/updateGroupChatMessage")
    public RestResponse<String> updateGroupChatMessage(@RequestBody GroupChatDTO groupChatMessageDTO) {
        return groupChatService.updateGroupChatMessage(groupChatMessageDTO);
    }


    /**************************************后台管理接口***********************************************/

    @ApiOperation("获取群聊列表")
    @GetMapping("/admin/groupChat")
    public PageResult<GroupChatAdminDTO> groupChatList(ConditionVO conditionVO) {
        return groupChatService.groupChatList(conditionVO);
    }

    @ApiOperation("管理员封禁或解封群聊")
    @PutMapping("/admin/operateStatus/{id}/{status}")
    public RestResponse<String> operateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return groupChatService.operateStatus(id, status);
    }


}

